/**
 * 15/02/2014
 * 
 * DelegationController
 */
package org.cuatrovientos.erp.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.apache.log4j.Logger;
import org.cuatrovientos.erp.business.DelegationBOInterface;
import org.cuatrovientos.erp.model.Delegation;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * Controller class form Delegation management
 * @author Javier Tejedor
 */
@Controller
public class DelegationController {

	//Get log4j logger
	private static final Logger logger = 
			Logger.getLogger(DelegationController.class);

	@Autowired
	private DelegationBOInterface delegationBO;

	/**
	 * default constructor
	 */
	public DelegationController () {
		logger.info("Delegation BEAN instantiated");
	}

	/**
	 * handles default /delegation or /delegation/list request
	 * @return
	 */
	@RequestMapping({"/delegation","*","/delegation/list"})
	public ModelAndView delegationDefault() {
		logger.info("default");

		Map<String, Object> model = new HashMap<String, Object>();

		List<Delegation> delegationList = delegationBO.selectAll();
		model.put("delegations", delegationList );
		
		return new ModelAndView("DelegationList", model);     
	}


	/**
	 * handles default /delegation/search request
	 * @param name
	 * @return
	 */
	@RequestMapping(method=RequestMethod.POST,value={"/delegation/search"})
	public ModelAndView search(
			@RequestParam(value="name", defaultValue="", required=true) 
			String description) {
		
		logger.info("search");

		Map<String, Object> model = new HashMap<String, Object>();

		List<Delegation> delegationList = delegationBO.search(description);
		model.put("delegations", delegationList );
		
		return new ModelAndView("DelegationList", model);     
	}

	/**
	 * handles default /delegation/delete request
	 * @param id
	 * @return
	 */
	@RequestMapping(method=RequestMethod.GET,value={"/delegation/delete"})
	public String deleteDelegation(@RequestParam(value="id", defaultValue="", required=true) int id) {
		logger.info("delete " + id);

		delegationBO.deleteById(id);
		
		return "redirect:/delegation";
	}

	/**
	 * handles default /delegation/new request
	 * @return
	 */
	@RequestMapping(method=RequestMethod.GET,value={"/delegation/new"})
	public ModelAndView newDelegation() {
		logger.info("new delegation");
		
		Delegation delegation = new Delegation();

		return new ModelAndView("NewDelegation", "delegation", delegation);
	}

	/**
	 * handles default /delegation/save request
	 * @param delegation
	 * @param bindingResult
	 * @return
	 */
	@RequestMapping(method=RequestMethod.POST,value={"/delegation/save"})
	public String newDelegation(@Valid Delegation delegation, BindingResult bindingResult) {
		logger.info("new delegation " + delegation.toString());

		//In case of validation errors, go back to form
		if (bindingResult.hasErrors()) {
			Map<String, Object> model = new HashMap<String, Object>();
			model.put("delegation", delegation);
			
			return "NewDelegation";
		}

		delegationBO.insert(delegation);
		
		return "redirect:/delegation";
	}

	/**
	 * handles default /delegation/detail request
	 * @param id
	 * @return
	 */
	@RequestMapping(method=RequestMethod.GET,value={"/delegation/detail"})
	public ModelAndView delegationDetail(@RequestParam(value="id", defaultValue="", required=true) int id) {
		logger.info("Detail " + id);
		
		Map<String,Object> model = new HashMap<String,Object>();
		Delegation delegation = delegationBO.selectById(id);
		model.put("delegation", delegation);

		return new ModelAndView("DelegationDetail", model);
	}


	/**
	 * handles default /delegation/update request
	 * @param id
	 * @return
	 */
	@RequestMapping(method=RequestMethod.GET,value={"/delegation/update"})
	public ModelAndView update(@RequestParam(value="id", defaultValue="", required=true) int id) {
		logger.info("update " + id);
		
		Map<String,Object> model = new HashMap<String,Object>();
		Delegation delegation = delegationBO.selectById(id);
		model.put("delegation", delegation);
		
		return new ModelAndView("UpdateDelegation",model);
	}


	/**
	 * handles default /delegation/saveupdate request
	 * @param delegation
	 * @param bindingResult
	 * @return
	 */
	@RequestMapping(method=RequestMethod.POST,value={"/delegation/saveupdate"})
	public String saveUpdate(@Valid Delegation delegation, BindingResult bindingResult) {
		logger.info("update delegation " + delegation.toString());

		// In case of validation errors, go back to form
		if (bindingResult.hasErrors()) {
			Map<String, Object> model = new HashMap<String, Object>();
			model.put("delegation", delegation);
			
			return "UpdateDelegation";
		}

		delegationBO.update(delegation);
		
		return "redirect:/delegation";
	}
	
	/**
	 * @param model
	 * @return
	 */
	@RequestMapping({"/delegation/index"})
	public String sayHelloPage(Map<String, Object> model) {
		return "index/IndexIssuetype";
	}
	
}
